package com.rimi.cs.javaweb.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rimi.cs.javaweb.pojo.Cart;
import com.rimi.cs.javaweb.pojo.Mobile;
import com.rimi.cs.javaweb.service.ICartService;
import com.rimi.cs.javaweb.service.IMobileService;
import com.rimi.cs.javaweb.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 访问的控制器
 *
 * @author Manka
 * @date 2018-06-26 15:35
 */
@Controller
public class JumpController {

    @Autowired
    private ICartService cartService;

    @Autowired
    private IMobileService mobileService;

    /**
     * 跳转首页
     * @param
     * @return
     */
    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index() {
        System.out.println("index");
        return "index";
    }

    /**
     * 跳转product页面
     * @return
     */
    @RequestMapping(value = "/product/{mid}",method = RequestMethod.GET)
    public String product(@PathVariable Long mid,Model model) {
        System.out.println("product "+mid);
        Mobile mobile = mobileService.selectItemById(mid);
        model.addAttribute("mobile",mobile);
        System.out.println(mobile);
        return "product";
    }

    /**
     * 跳转contact页面
     * @return
     */
    @RequestMapping(value = "/contact",method = RequestMethod.GET)
    public String contact() {
        System.out.println("contact");
        return "contact";
    }

    /**
     * 跳转category页面
     * @return
     */
    @RequestMapping(value = "/category",method = RequestMethod.GET)
    public String category(Model model, @RequestParam(required = false, defaultValue = "1") int pageIndex, @RequestParam(required = false, defaultValue = "") String searchItem) {
        System.out.println("category");
        // 分页
        PageHelper.startPage(pageIndex,Constant.PAGE_SIZE);
        // 调用业务层的方法
        List<Mobile> mobileList = mobileService.selectPhoneItems(searchItem);
        // 返回结果数据
        model.addAttribute("page", new PageInfo<Mobile>(mobileList));
        return "category";
    }

    /**
     * 跳转cart页面
     * @return
     */
    @RequestMapping(value = "/cart",method = RequestMethod.GET)
    public String cart(Model model, HttpServletRequest request) {
        System.out.println("cart");
        List<Cart> cartList = cartService.selectByUserId(request);
        model.addAttribute("cartList",cartList);
        return "cart";
    }

    /**
     * 跳转account页面
     * @return
     */
    @RequestMapping(value = "/account",method = RequestMethod.GET)
    public String account() {
        System.out.println("account");
        return "account";
    }

    /**
     * 跳转imformation页面
     * @return
     */
    @RequestMapping(value = "/information",method = RequestMethod.GET)
    public String imformation() {
        System.out.println("information");

        return "information";
    }
    /**
     * 跳转到forgotPassword页面
     * @return
     */
    @RequestMapping(value = "/forgotPassword", method = RequestMethod.GET)
    public String forgetPassword(){
        return "forgotPassword";
    }

    /**
     * 跳转到resetPassword页面
     * @return
     */
    @RequestMapping(value = "/resetPassword", method = RequestMethod.GET)
    public String resetPassword(){
        return "resetPassword";
    }

    /**
     * 发生500错误时跳转
     * @return
     */
    @RequestMapping("/error500")
    public String error500(){
        return "error/500";
    }

    /**
     * 发生404错误时跳转
     * @return
     */
    @RequestMapping("/error404")
    public String error404(){
        return "error/404";
    }
}
